﻿using Spire.Pdf;
using Spire.Pdf.Security;
using System;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmPdfDecryption : Form
    {
        public FrmPdfDecryption()
        {
            InitializeComponent();
        }

        private void btnOpenfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "打开PDF";
            fileDialog.Filter = "pdf文件|*.pdf";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string name = fileDialog.FileName;
                txtFilePath.Text = name;
            }
        }

        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "选择保存路径";
            saveFile.Filter = "pdf文件|*.pdf";
            saveFile.DefaultExt = ".pdf";
            saveFile.FileName = "解密的PDF.pdf";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = saveFile.FileName;
            }
        }

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            if (string.IsNullOrEmpty(txtSavePath.Text))
            {
                MessageBox.Show("请先选择解密后的pdf保存路径！");
                return;
            }
            try
            {
                //使用密码加载一个加密的PDF文档
                PdfDocument pdf = new PdfDocument();
                pdf.LoadFromFile(txtFilePath.Text, "");

                // 过时
                ////取消文档打开口令
                //pdf.Security.UserPassword = string.Empty;
                ////取消许可口令
                //pdf.Security.OwnerPassword = string.Empty;
                ////重置文档权限到默认状态
                //pdf.Security.Permissions = PdfPermissionsFlags.Default;
                //pdf.Security.ResetPermissions(PdfPermissionsFlags.Default);
                //pdf.Security.Encrypt(string.Empty);


                //将密码设置为空以解密PDF
                pdf.Security.Encrypt(string.Empty, string.Empty, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permission");

                //保存文档
                pdf.SaveToFile(txtSavePath.Text, FileFormat.PDF);

                MessageBox.Show("完成");
            }
            catch (Exception exp)
            {
                MessageBox.Show("出错：" + exp.Message);
            }
        }


        private void btnOpenThisPdf_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!System.IO.File.Exists(txtSavePath.Text))
            {
                return;
            }
            else
            {
                //存在文件
                System.Diagnostics.Process.Start(txtSavePath.Text);
            }
        }

        /// <summary>
        /// 开始拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.txtFilePath.Cursor = Cursors.Arrow; //指定鼠标形状   
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        /// <summary>
        /// 接收拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (Path.GetExtension(path).ToLower() == ".pdf")
            {
                this.txtFilePath.Text = path;
            }
            this.txtFilePath.Cursor = Cursors.IBeam; //还原鼠标形状 
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
